<template>
	<view class="scrool-page">
		<view>
			<uni-row class="demo-uni-row">
				<uni-col :span="8">
					<view class="title">源库位</view>
					<input type="text" v-model="sourceCode"
						style="height: 50rpx;border: 1px;background-color: white;" />
				</uni-col>
				<uni-col :span="8" :offset="4">
					<view class="title">目标库位</view>
					<input type="text" v-model="targetCode"
						style="height: 50rpx;border: 1px;background-color: white;" />
				</uni-col>
			</uni-row>
		</view>
		<view class="list-cont">
			<scroll-view class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(item,index) in productList" :key="index">
					<uni-card :title="'序号:'+(index+1)">
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>
				</view>
			</scroll-view>
		</view>

		<view class="footer">
			<button type="default" size='mini' @click="reduceData" style="width:48%; margin-left:1%;"
				v-if="!userflag">减码</button>
			<button type="default" size='mini' @click="reduceData" style="width:48%; margin-left:1%;" v-else>加箱</button>
			<button type="default" size='mini' @click="submit" style="width:49%; margin-left:1%;">完成</button>
		</view>

		<uni-popup ref="dataDialog" type="dialog">
			<uni-popup-dialog mode="input" title="请输入本次移动的箱数" :value='dataValue' :before-close="true"
				@confirm="dataConfirm" @close="dataClose">
			</uni-popup-dialog>
		</uni-popup>
	</view>

</template>

<script>
	import {
		getToken
	} from '@/utils/auth';
	import common from '../../pages/common/common.js'
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	export default {
		data() {
			return {
				// 默认显示0
				sourceCode: '', //原货架
				targetCode: '', //目标货架
				productList: [], //转移的货品
				userflag: false,
				baseUrl: getApp().globalData.config.baseUrl,
				userName: this.$store.state.user.name,
				orgId: getApp().globalData.config.orgId,
				dataValue: '0',
				codeList: [],
			}
		},

		onShow() {
			this.orgId = uni.getStorageSync('orgId');
			scanDevice = new ScanDeviceClass();
			scanDevice.openScan(); // 打开扫描
			scanDevice.setOutScanMode(0); // 扫描模式				
			this.initScan();
			this.registerScan();
		},

		onHide: function() {
			this.unregisterScan();
		},

		destroyed: function() {
			this.unregisterScan();
		},

		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
				return false;
			}
			//that.unregisterScan();
			uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
				delta: 1
			});
			return true;
		},

		methods: {
			initScan() {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				filter.addAction("scan.rcv.message"); // 换你的广播动作  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getByteArrayExtra('barocode');
						let codeStr = common.utf8ByteToUnicodeStr(code);
						scanDevice.stopScan(); // 停止扫描	
						_this.buttonScan(codeStr);
						_this.viewScan(codeStr);
					}
				});
			},

			registerScan() {
				main.registerReceiver(receiver, filter);
			},

			unregisterScan() {
				main.unregisterReceiver(receiver);
			},

			buttonScan(codeStr) {
				if (this.userflag) {
					return;
				}
				//显示加载框
				uni.showLoading({
					title: "加载中",
					mask: true
				});
				var result = common.scanCode(codeStr);
				console.log(codeStr)
				console.log(result)
				if (result[0] == "K") {
					if (this.sourceCode == null || this.sourceCode == '') {
						if (this.targetCode == result[1]) { //如果源库区和目标库区相同报错
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: '目标库区和源库区不能相同',
								icon: 'none',
								duration: 3000
							});
							return;
						} else {
							console.log(result[1])
							this.judgeAreaCode(result[1], true); //一定是先扫描源库区
							//this.sourceCode = codeStr;
						}

						if (this.targetCode == result[0] && codeStr == 'CH') { //CH库位单独逻辑
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: '目标库区和源库区不能相同',
								icon: 'none',
								duration: 3000
							});
							return;
						} else if (this.targetCode != result[0] && codeStr == 'CH') {
							//console.log(result[1])
							this.judgeAreaCode(codeStr, true); //一定是先扫描源库区
							//this.sourceCode = codeStr;
						}

					} else {
						if (this.sourceCode == result[1]) { //如果源库区和目标库区相同报错
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: '目标库区和源库区不能相同',
								icon: 'none',
								duration: 3000
							});
							return;
						} else {
							console.log(result[1])
							this.judgeAreaCode(result[1], false);
							//this.targetCode = codeStr;
						}

						if (this.sourceCode == result[0] && codeStr == 'CH') { //CH库位单独逻辑
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: '目标库区和源库区不能相同',
								icon: 'none',
								duration: 3000
							});
							return;
						} else if (this.sourceCode != result[0] && codeStr == 'CH') {
							//console.log(result[1])
							this.judgeAreaCode(codeStr, true); //一定是先扫描源库区
							//this.sourceCode = codeStr;
						}
					}
				} else if (result[0] == "C") {
					if (this.sourceCode == null || this.sourceCode == '' || this.targetCode == null || this.targetCode ==
						'') {
						//隐藏加载框
						uni.hideLoading();
						uni.showToast({
							title: '请先扫描目标库区和源库区',
							icon: 'none',
							duration: 3000
						});
						return;
					}
					var flag = false;
					for (var i = 0; i < this.productList.length; i++) {
						if (this.productList[i].boxCode == result[1]) {
							flag = true;
							break;
						}
					}
					if (flag) {
						//隐藏加载框
						uni.hideLoading();
						uni.showToast({
							title: '此箱码已经扫描',
							icon: 'none',
							duration: 3000
						});
						return;
					} else {
						if (this.codeList.find(item => item === result[1])) {
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: '此箱码已经扫描',
								icon: 'none',
								duration: 3000
							});
							return;
						} else {
							this.codeList.push(result[1]);
							this.judgeBoxCode(result[1]);
						}

					}
					//隐藏加载框
					uni.hideLoading();
				} else {
					//隐藏加载框
					uni.hideLoading();
					uni.showToast({
						title: '请正确扫描编码',
						icon: 'none',
						duration: 3000
					});
					return;
				}
			},

			//判断库位是否存在或者可用
			judgeAreaCode(codeStr, flag) {
				console.log(codeStr)
				uni.request({
					url: this.baseUrl + '/system/WmsStockLog/verify',
					data: {
						areaCode: codeStr,
						orgId: this.orgId,
						userName: this.userName,
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						//隐藏加载框
						uni.hideLoading();
						if (res.data.code == '200' || res.data.code == 200) {
							if (flag) {
								this.sourceCode = codeStr;
							} else {
								this.targetCode = codeStr;
							}
						} else if (res.data.code == '201' || res.data.code == 201) {
							uni.showToast({
								title: '库位' + codeStr + "不存在或不可用",
								icon: 'none',
								duration: 5000
							});
							return;
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 5000
							});
							return;
						}

					},
				})
			},

			//判断此箱是否是源库位的一箱
			judgeBoxCode(codeStr) {
				uni.request({
					url: this.baseUrl + '/system/barcode/box',
					data: {
						boxCode: codeStr,
						orgId: this.orgId,
						userName: this.userName,
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						//隐藏加载框
						uni.hideLoading();
						if (res.data.code == '200' || res.data.code == 200) {
							var warehouseLocation = res.data.data.storageArea;
							if (warehouseLocation != this.sourceCode) {
								uni.showToast({
									title: '此箱' + codeStr + '的库位是' + warehouseLocation + '不属于源库位',
									icon: 'none',
									duration: 5000
								});
							} else {
								var arr = {
									boxCode: codeStr
								};
								this.productList.push(arr);
							}
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 5000
							});
							return;
						}

					},
				})
			},

			viewScan(codeStr) {
				if (!this.userflag) {
					return;
				}
				//显示加载框
				uni.showLoading({
					title: "加载中",
					mask: true
				});
				var result = common.scanCode(codeStr);
				if (result[0] != "C") {
					//隐藏加载框
					uni.hideLoading();
					uni.showToast({
						title: '请正确扫描编码',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				var tempList = [];
				for (var i = 0; i < this.productList.length; i++) {
					if (result[1] != this.productList[i].boxCode) {
						tempList.push(this.productList[i]);
					}
				}
				this.productList = tempList;
				//隐藏加载框
				uni.hideLoading();
			},

			reduceData() {
				if (this.userflag) {
					this.userflag = false;
				} else {
					this.userflag = true;
				}
			},

			dataConfirm(dataValue) {
				var that = this;
				//显示加载框
				uni.showLoading({
					title: "加载中",
					mask: true
				});
				uni.request({
					url: that.baseUrl + '/system/WmsStockLog/add1',
					data: {
						type: 'all',
						operator: that.userName,
						areaOld: that.sourceCode,
						areaNew: that.targetCode,
						number: dataValue,
						batchNumber: [],
						orgId: that.orgId,
						userName: that.userName,
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						//隐藏加载框
						uni.hideLoading();
						if (res.data.code == '200' || res.data.code == 200) {
							uni.showToast({
								title: '迁移成功',
								icon: 'success',
								duration: 3000
							});
							//that.unregisterScan();
							setTimeout(function() {
								uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							}, 1000);
						} else if (res.data.code == '201' || res.data.code == 201) {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 5000
							});
							return;
						}

					},
				})
			},

			dataClose() {
				this.$refs.dataDialog.close();
			},

			submit() {
				var that = this;
				if (that.productList.length == 0) {
					that.$refs.dataDialog.open();
				} else {
					//显示加载框
					uni.showLoading({
						title: "加载中",
						mask: true
					});
					uni.request({
						url: that.baseUrl + '/system/WmsStockLog/add1',
						data: {
							type: 'part',
							operator: that.userName,
							areaOld: that.sourceCode,
							areaNew: that.targetCode,
							number: '',
							batchNumber: that.productList,
							orgId: that.orgId,
							userName: that.userName,
						},
						method: 'POST',
						header: {
							'authorization': getToken()
						},
						dataType: 'json',
						success: (res) => {
							//隐藏加载框
							uni.hideLoading();
							if (res.data.code == '200' || res.data.code == 200) {
								//this.productList = res.data.data;
								//console.log(this.productList);
								uni.showToast({
									title: '迁移成功',
									icon: 'success',
									duration: 3000
								});
								//that.unregisterScan();
								setTimeout(function() {
									uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
										delta: 1
									});
								}, 1000);
							} else {
								uni.showToast({
									title: res.data.msg,
									icon: 'none',
									duration: 2000
								});
								return;
							}

						},
					})
				}
			},

		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}

	.text-content {
		font-size: 48rpx;
		text-align: center;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}

	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height: 30px;
		font-size: 26rpx;
	}

	page {
		width: 100%;
		height: 100%;
	}


	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}

	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}

	.list-cont {
		flex: 1;
		position: relative;

		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}

		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;

		}
	}


	.footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>